package be.donovan.gui.helper.components;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import org.jdesktop.swingx.JXTable;

import be.donovan.gui.helper.MainFrame;
import be.donovan.gui.helper.ServiceLocator;
import be.donovan.model.Catalog;
import be.donovan.model.Product;
import be.donovan.model.persist.PersistenceHelper;


/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("hiding")
public class CatalogEditorPanel extends JDialog {
	
	private static String[] getters = new String[] { "getCode", "getDescription" };

	private static String[] heading = new String[] { "Code", "Description" };

	private JPanel constraintsPanel;

	public JTextField codeTextField;

	public JButton searchButton;

	public JXTable resultList;

	private JLabel resultLabel;

	private JTextField descriptionTextField;

	private JLabel descriptionLabel;

	private JLabel codeLabel;

	private JScrollPane listPanel;

	private Class<Catalog> clazz = null;

	private Catalog initialValue = null;


	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new CatalogEditorPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public CatalogEditorPanel(Class<Catalog> clazz, Catalog initialValue) {
		this();
		this.clazz = clazz;
		this.initialValue = initialValue;
	}
	
	public CatalogEditorPanel() {
		super(MainFrame.getInstance());
		setModal(true);
		initGUI();

	}

	public CatalogEditorPanel(Class<? extends Catalog> class1) {
		this();
		this.clazz=(Class<Catalog>) class1;
	}

	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			this.setPreferredSize(new java.awt.Dimension(568, 422));
			thisLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 };
			thisLayout.rowHeights = new int[] { 7, 7, 7, 7, 7, 7, 7, 7 };
			thisLayout.columnWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
			thisLayout.columnWidths = new int[] { 7, 7, 7, 7 };
			this.setLayout(thisLayout);
			this.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent evt) {
					resultList.clearSelection();
				}
			});
			{
				constraintsPanel = new JPanel();
				GridBagLayout constraintsPanelLayout = new GridBagLayout();
				constraintsPanelLayout.rowWeights = new double[] { 0.1, 0.1 };
				constraintsPanelLayout.rowHeights = new int[] { 7, 7 };
				constraintsPanelLayout.columnWeights = new double[] { 0.1, 0.1, 0.1 };
				constraintsPanelLayout.columnWidths = new int[] { 7, 7, 7 };
				constraintsPanel.setLayout(constraintsPanelLayout);
				this.add(constraintsPanel, new GridBagConstraints(0, 0, 4, 1, 0.0, 0.0, GridBagConstraints.EAST,
						GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
				{
					codeLabel = new JLabel();
					BorderLayout codeLabelLayout = new BorderLayout();
					codeLabel.setLayout(codeLabelLayout);
					constraintsPanel.add(codeLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
					codeLabel.setText("Code");
				}
				{
					codeTextField = new JTextField();
					constraintsPanel.add(codeTextField, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
				}
				{
					descriptionLabel = new JLabel();
					constraintsPanel.add(descriptionLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
					descriptionLabel.setText("Description");
				}
				{
					descriptionTextField = new JTextField();
					constraintsPanel.add(descriptionTextField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
				}
				{
					searchButton = new JButton();
					constraintsPanel.add(searchButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					searchButton.setText("Search");
					searchButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							search();
						}
					});
				}
				this.getRootPane().setDefaultButton(searchButton);
			}
			{
				listPanel = new JScrollPane();
				getContentPane().add(
						listPanel,
						new GridBagConstraints(1, 2, 2, 6, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH,
								new Insets(5, 0, 5, 0), 0, 0));
				{
					resultList = new JXTable();
					listPanel.setViewportView(resultList);
					resultList.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							if (evt.getClickCount() > 1) {
								dispose();
								setVisible(false);
							}
						}
					});
				}
			}
			{
				resultLabel = new JLabel();
				this.add(resultLabel, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER,
						GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				resultLabel.setText("Search Results : Double-Click To Select");
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Catalog start(Component superComp) {
		this.setLocationRelativeTo(superComp);
		this.setVisible(true);
		return (resultList.getSelectedRow() != -1) ? ((MVCTableModel<Catalog>) resultList.getModel()).findByRow(resultList
				.convertRowIndexToModel(resultList.getSelectedRow())) : initialValue;
	}
	
	private void search() {
		Set<Catalog> results = null;
		try {
			results = new TreeSet<Catalog>(ServiceLocator.getService(PersistenceHelper.class).findCatalogs(clazz, descriptionTextField.getText()));
			CatalogRegistry.add(clazz, results);
			final MVCTableModel<Catalog> model = new MVCTableModel<Catalog>(results, new Vector<String>(Arrays
					.asList(getters)), new Vector<String>(Arrays.asList(heading)));
			resultList.setModel(model);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(e.getMessage());
		}
	}
	
}
